home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / misc / unix / tracker_4_3.lzh / tracker / Amiga / popen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-13  |  2.5 KB  |  117 lines

  1. /* amiga/popen.c 
  2.     vi:se ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: popen.c,v 1.6 1994/01/09 17:38:28 Espie Exp Espie $
  6.  * $Log: popen.c,v $
  7.  * Revision 1.6  1994/01/09  17:38:28  Espie
  8.  * Generalized open.c.
  9.  *
  10.  * Revision 1.5  1994/01/07  15:08:54  Espie
  11.  * Maybe correct code now...
  12.  *
  13.  * Revision 1.4  1994/01/06  22:37:26  Espie
  14.  * Fixed up some cosmetic problems.
  15.  *
  16.  * Revision 1.3  1994/01/05  14:55:38  Espie
  17.  * *** empty log message ***
  18.  *
  19.  * Revision 1.2  1993/12/28  14:03:53  Espie
  20.  * *** empty log message ***
  21.  *
  22.  * Revision 1.1  1993/12/26  22:48:18  Espie
  23.  * Initial revision
  24.  *
  25.  */
  26.  
  27.  
  28. #include <proto/dos.h>
  29. #include <proto/exec.h>
  30. #include <exec/tasks.h>
  31. #include <dos/dostags.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include "defs.h"
  35. ID("$Id: popen.c,v 1.6 1994/01/09 17:38:28 Espie Exp Espie $")
  36.  
  37. /*
  38. ###   CSupport/popen
  39. ###
  40. ###   NAME
  41. ###      popen/pclose -- Unix-like pipes
  42. ###
  43. ###   STATUS
  44. ###      Experimental
  45. ###      does not work with csh !
  46. ###
  47.  */
  48. FILE *popen(char *command, char *mode)
  49.    {
  50.    static char pname[25];
  51.    struct Task *me = FindTask(0);
  52.    static count = 0;
  53.    
  54.    count++;
  55.    
  56.       /* guarantees a unique pipe name ! */
  57.    sprintf(pname, "pipe:tr_%lx_%d", me, count);
  58.    
  59.    if (strcmp(mode, "r") == 0)
  60.       /* open pipe for reading */
  61.       {
  62.       FILE *reader;
  63.       BPTR writer, null;
  64.  
  65.       writer = Open(pname, MODE_NEWFILE);
  66.       reader = fopen(pname, "r");
  67.       null = Open("NIL:", MODE_NEWFILE);
  68.       if (SystemTags(command, SYS_Input, null, 
  69.          SYS_Output, writer, SYS_Asynch, TRUE, 
  70.          NP_StackSize, me->tc_SPUpper - me->tc_SPLower,
  71.          TAG_END) == -1)
  72.          {
  73.          Close(null);
  74.          Close(writer);
  75.          fclose(reader);
  76.          return NULL;
  77.          }
  78.       else
  79.          return reader;
  80.       }
  81.    else if (strcmp(mode, "w") == 0)
  82.       /* open pipe for writing */
  83.       {
  84.       FILE *writer;
  85.       BPTR reader, null;
  86.       
  87.       writer = fopen(pname, "w");
  88.       reader = Open(pname, MODE_OLDFILE);
  89.       null = Open("NIL:", MODE_NEWFILE);
  90.       if (SystemTags(command, SYS_Input, reader, 
  91.          SYS_Output, null, SYS_Asynch, TRUE, 
  92.          NP_StackSize, me->tc_SPUpper - me->tc_SPLower, 
  93.          TAG_END) == -1)
  94.          {
  95.          Close(null);
  96.          Close(reader);
  97.          fclose(writer);
  98.          return NULL;
  99.          }
  100.       else
  101.          return writer;
  102.       }
  103.    else
  104.       return NULL;
  105.    }
  106.  
  107. /* for us, pclose is just fclose.
  108.  * But we have to insure the file is empty first
  109.  */
  110. void pclose(FILE *f)
  111.    {
  112.    while (fgetc(f) != EOF)
  113.       ;
  114.    fclose(f);
  115.    }
  116.  
  117.